package com.ruoyi.web.controller.system;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TbArticle;
import com.ruoyi.system.service.ITbArticleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 新闻管理Controller
 * 
 * @author ruoyi
 * @date 2025-03-31
 */
@RestController
@RequestMapping("/system/article")
public class TbArticleController extends BaseController
{
    @Autowired
    private ITbArticleService tbArticleService;

    /**
     * 查询新闻管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbArticle tbArticle)
    {
        startPage();
        List<TbArticle> list = tbArticleService.selectTbArticleList(tbArticle);
        return getDataTable(list);
    }

    /**
     * 导出新闻管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:article:export')")
    @Log(title = "新闻管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbArticle tbArticle)
    {
        List<TbArticle> list = tbArticleService.selectTbArticleList(tbArticle);
        ExcelUtil<TbArticle> util = new ExcelUtil<TbArticle>(TbArticle.class);
        util.exportExcel(response, list, "新闻管理数据");
    }

    /**
     * 获取新闻管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:article:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(tbArticleService.selectTbArticleById(id));
    }

    /**
     * 新增新闻管理
     */
    @PreAuthorize("@ss.hasPermi('system:article:add')")
    @Log(title = "新闻管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbArticle tbArticle)
    {
        return toAjax(tbArticleService.insertTbArticle(tbArticle));
    }

    /**
     * 修改新闻管理
     */
    @PreAuthorize("@ss.hasPermi('system:article:edit')")
    @Log(title = "新闻管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbArticle tbArticle)
    {
        return toAjax(tbArticleService.updateTbArticle(tbArticle));
    }

    /**
     * 删除新闻管理
     */
    @PreAuthorize("@ss.hasPermi('system:article:remove')")
    @Log(title = "新闻管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(tbArticleService.deleteTbArticleByIds(ids));
    }


    @PostMapping(value = "/releaseArticle")
    public AjaxResult releaseArticle(@RequestBody  TbArticle tbArticle) throws Exception {
        //tbArticle
       // startPage();
        tbArticleService.updateTbArticle(tbArticle);
        tbArticle.setTbState("normal");
        List<TbArticle> list = tbArticleService.selectTbArticleListLast(tbArticle);
        updateIndexNews(list);
        tbArticle=tbArticleService.selectTbArticleById(tbArticle.getId());
        genNewsDetail(tbArticle);
        return success(list);
    }





    ////////////更新首页的按个新闻
    public void updateIndexNews( List<TbArticle> list) throws Exception {
        //1.创建配置类
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_31);
        //
        configuration.setDirectoryForTemplateLoading(new File("D:\\project\\东方巨龙\\developCode\\dfjloffice\\RuoYi-App\\ruoyi-admin\\src\\main\\resources"));
        //3.设置字符集
        configuration.setDefaultEncoding("utf-8");
        //4.加载模板
        Template template = configuration.getTemplate("index.ftl");

        //5.创建数据模型
        Map map=new HashMap();
        map.put("name", "张三");
        map.put("title", "欢迎来使用Freemarker！");
        map.put("content", "欢迎来使用Freemarker！");
        map.put("articleList", list);
        //6.创建Writer对象
        Writer out =new FileWriter(new File("D:\\project\\东方巨龙\\wwwroot\\hello.html"));
        //7.输出
        template.process(map, out);
        //8.关闭Writer对象
        out.close();
    }


    ////////////更新新闻列表
    public void updateNewslist() throws IOException, TemplateException {
        //1.创建配置类
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_31);
        //
        configuration.setDirectoryForTemplateLoading(new File("D:\\project\\东方巨龙\\developCode\\dfjloffice\\RuoYi-App\\ruoyi-admin\\src\\main\\resources"));
        //3.设置字符集
        configuration.setDefaultEncoding("utf-8");
        //4.加载模板
        Template template = configuration.getTemplate("index.ftl");

        //5.创建数据模型
        Map map=new HashMap();
        map.put("name", "张三");
        map.put("title", "欢迎来使用Freemarker！");
        map.put("content", "欢迎来使用Freemarker！");
        //6.创建Writer对象
        Writer out =new FileWriter(new File("D:\\project\\东方巨龙\\wwwroot\\hello.html"));
        //7.输出
        template.process(map, out);
        //8.关闭Writer对象
        out.close();
    }


    ////////////生成新闻列表详情
    public void  genNewsDetail(TbArticle tbArticle) throws IOException, TemplateException {
        //1.创建配置类
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_31);
        //
        configuration.setDirectoryForTemplateLoading(new File("D:\\project\\东方巨龙\\developCode\\dfjloffice\\RuoYi-App\\ruoyi-admin\\src\\main\\resources"));
        //3.设置字符集
        configuration.setDefaultEncoding("utf-8");
        //4.加载模板
        Template template = configuration.getTemplate("newsDetail.ftl");

        //5.创建数据模型
        Map map=new HashMap();
//        map.put("name", "张三");
//        map.put("title", "欢迎来使用Freemarker！");
           map.put("tbArticle", tbArticle);
        //6.创建Writer对象
            Writer out =new FileWriter(new File("D:\\project\\东方巨龙\\wwwroot\\xinwenzixun\\news_"+tbArticle.getId()+".html"));
        //7.输出
        template.process(map, out);
        //8.关闭Writer对象
        out.close();
    }




}
